package org.funtester.performance.books.chapter01.section3;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 使用{@link ExecutorService}实现Java 缓存线程池（Cached Thread Pool）
 */
public class CachedThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();// 创建一个缓存线程池
        for (int i = 0; i < 8; i++) {// 循环8次
            // 提交任务
            executorService.execute(() -> {
                try {
                    Thread.sleep(100);// 睡眠100毫秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + "  Hello FunTester!  " + Thread.currentThread().getName());// 输出当前时间和线程名
            });

        }
        executorService.shutdown();// 关闭线程池
    }

}
